﻿using JTTD.Common.Attribute;
using JTTD.Common.Helper;
using JTTD.Domain;
using JTTD.Domain.Dtos;
using JTTD.Domain.Dtos.CompanyBill.Request;
using JTTD.Domain.Dtos.CompanyBill.Response;
using JTTD.Domain.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace JTTD.Controllers
{
    /// <summary>
    /// 商家费用
    /// </summary>
    [Route("api/companyBill")]
    [ApiController]
    public class CompanyBillController : BaseController
    {
        private readonly ICompanyBillService _companyBillService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="companyBillService"></param>
        public CompanyBillController(ICompanyBillService companyBillService)
        {
            _companyBillService = companyBillService;
        }

        /// <summary>
        /// 获取指定店铺没有支付的账单(管理员后台)
        /// </summary>
        /// <param name="shopId">店家id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("{shopId}/noPayBills")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<List<CompanyBillResponseDto>>))]
        [UserRoot(UserType=Common.Enum.UserTypeEnum.Admin)]
        public async Task<IActionResult> GetNoPayBillList([FromRoute]int shopId)
        {
            var result = await _companyBillService.GetNoPayBillList(shopId);
            return Ok(result);
        }

        /// <summary>
        /// 分页获取当前登录店铺的所有账单
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<PageResult<CompanyBillResponseDto>>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Shop)]
        public async Task<IActionResult> GetPageList([FromQuery]SearchCompanyBillDto search)
        {
            var result = await _companyBillService.GetPageList(UserId,search);
            return Ok(result);
        }

        /// <summary>
        /// 设置账单(管理员后台)
        /// </summary>
        /// <param name="shopId">店家id</param>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("{shopId}/upload")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Admin)]
        public async Task<IActionResult> SetCompanyBill([FromRoute]int shopId,IFormFile file)
        {
            var dtos= ExcelHelper.ImportExcel(file).ToList<SetCompanyBillRequestDto>();
            var result = await _companyBillService.SetCompanyBill(shopId,dtos);
            return Ok(result);
        }

        /// <summary>
        /// 确认账单已经支付(管理员后台)
        /// </summary>
        /// <param name="billId">账单id</param>
        /// <returns></returns>
        [HttpPut]
        [Route("{billId}")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Admin)]
        public async Task<IActionResult> ConfirmBill([FromRoute]int billId)
        {
            var result = await _companyBillService.ConfirmBill(billId);
            return Ok(result);
        }

        /// <summary>
        /// 作废账单(管理员后台)
        /// </summary>
        /// <param name="billId">账单id</param>
        /// <returns></returns>
        [HttpDelete]
        [Route("{billId}")]
        [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(ApiResult<bool>))]
        [UserRoot(UserType = Common.Enum.UserTypeEnum.Admin)]
        public async Task<IActionResult> DeleteCompanyBill([FromRoute]int billId)
        {
            var result = await _companyBillService.DeleteCompanyBill(billId);
            return Ok(result);
        }

       
    }
}
